import {observable, action} from 'mobx';
import {Tools} from 'weaver-mobile';
import { Browser,ListViewPage } from 'weaver-mobile-page';
import * as API from '../../api/systemdoc';
import AddStore from '../docForm/AddStore';
const getLabel = Tools.getLabel;

export default class CategoryStore {
  
  systemDocStore = null;
  @observable child = null;
  @observable addStore;
  
  // 目录模式下的当前目录
  categoryList = [];
  // 目录模式下的文档列表
  categoryDocList = [];
  // 目录模式下文档列表的页码
  categoryDocPageNum = 1;
  // 目录模式下的列表数据源
  @observable categoryListViewPageStore = new ListViewPage.Store({ route: '/systemDoc' });
  
  @observable category = null;  
  @observable browser;
  @observable parentid = 0; 
  parentStore = null; 
  
  constructor(params) {
    if (params) {
      const {category, systemDocStore,parentStore} = params;
      this.parentStore = parentStore;
      this.systemDocStore = systemDocStore;
      if (params.category) {
        this.category = {id: category.sid, name: category.sname,pid: category.pid};
      } else {
        this.category = {
          name: this.systemDocStore.tabKey === 'all' ? getLabel(30898, '全部文档') : getLabel(1212, '我的文档')
        };
      }
    }
  }
  
  @action
  initPath = (path) => {
    this.parentid = this.category.pid || -1;
    this.browser = new Browser.Store({ route: path + '/tree', id: this.category.id || 'root' });
    this.addStore = new AddStore({
      parent: this,
      path: `${path}/add`,
      categoryId: this.category.id,
      saveBackFunc: this.addBack,
    });
    // this.category.name = this.systemDocStore.tabKey === 'all'? getLabel(30898, '全部文档') : getLabel(1212, '我的文档');
  }
   //将已经阅读过的设置已阅，取消阅读标识,每看一篇文档，将其父层目录的未读数量减一
   setDocIsNew = (rowData) =>{
    let docid = rowData.docid;
    //判断当前文档是否已阅，如果未阅才对目录的未读数进行计算
    let docIsNew = false;
    this.categoryListViewPageStore.data.map((val)=>{
      if(val.docid && val.docid===docid){
        docIsNew = val.isnew !== '0';
        val.isnew = '0';
      }
      return val;
    })
    this.categoryListViewPageStore.updateDataSource();
    //判断当前路由的层级
    if(docIsNew){
      this.changeFolderNoReadNum(this.parentStore,this.category);
    }
  }
  //递归修改父目录的阅读数
  changeFolderNoReadNum = (parentstore,category) =>{
    parentstore.categoryListViewPageStore.data.map((val) => {
      if(val.type === 'category' && val.sid === category.id && parseInt(val.noReadNum,10)>0){
        val.noReadNum = (parseInt(val.noReadNum,10) - 1) + '' ;
      }
      return val;
    })
    if(parentstore.parentStore){
      this.changeFolderNoReadNum(parentstore.parentStore,parentstore.category);
    }
    parentstore.categoryListViewPageStore.updateDataSource();
  }
  //下拉刷新
  refreshLoading = () => {
    this.categoryListViewPageStore.setState({refreshLoading:true});
    this.getListWithCategory();
  }
  // 目录模式的读取第一页与目录数据
  getListWithCategory = () => {
    //当前目录下的子数据
    this.categoryListViewPageStore.setState({hasMore: true, loading: true});
    this.categoryListViewPageStore.updateDataSource();
    this.child = null;
    let params = {categoryid: this.category ? this.category.id : '',scope:this.systemDocStore.scope};
    let promise = this.systemDocStore.tabKey === 'all' ? API.getCategoryList(params) : API.getMyCategoryList(params);
    return promise.then((result) => {
      this.categoryList = result.categorys.map((item) => {return {...item, type: 'category'}});
      this.categoryDocPageNum = 0;
      this.categoryDocList = [];
      return this.getNextPageWithCategory();
    });
  }
  
  // 目录模式的读取下一页的文档列表
  getNextPageWithCategory = () => {
    this.categoryListViewPageStore.setState({loading: true});
    this.categoryDocPageNum++;
    let promise;
    // 根目录不读取文档
    if (this.category && this.category.id) {
      let params = {categoryid:this.category.id,pageNum: this.categoryDocPageNum, pageSize: 20,scope:this.systemDocStore.scope};
      if(this.systemDocStore.tabKey === 'my'){
        params = {...params,myDoc:'1'};
      }
      promise = API.getCategoryDocList(params).then((result) => {
        this.categoryDocList = [...this.categoryDocList, ...result.docs.map((item) => {return {...item, type: 'doc'}})];
        return result;
      });
     
    } else {
      promise = new Promise((resolve) => resolve());
    }
    return promise.then((result) => {
      this.categoryListViewPageStore.setState({
        loading: false,
        data: [...this.categoryList, ...this.categoryDocList],
        isTable: false,
        hasMore: result && result.docs.length > 0,
      });
      this.categoryListViewPageStore.updateDataSource();
    });
  }
  
  //给当前父目录设置其下的子目录，如果当前目录下没有子目录，则证明走到了目录的最底层，跳出路由循环
  setChild = (category,store) => {
    this.child = new CategoryStore({category, systemDocStore: this.systemDocStore,parentStore:store});
    this.child.getListWithCategory();
  }
  getCurrentId = () => {
    if (this.child) {
      return this.child.getCurrentId();
    }
    return this.category.id;
  }
  
  addBack = ({id, history}) => {
    this.getListWithCategory();
    history.go(-1);
  }
}